package com.mcb_wxc.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 时间处理工具类
 *
 * @author 许智杰
 * @version v3.0    2014年5月12日
 */
@SuppressWarnings("static-access")
public class DateFormatUtil {
    public static SimpleDateFormat Sec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat Min = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat common = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat commonLong = new SimpleDateFormat("yyyy年MM月dd日");
    public static SimpleDateFormat commonShort = new SimpleDateFormat("MM月dd日");
    public static SimpleDateFormat dayFormat = new SimpleDateFormat("MM-dd");
    public static String[] WEEKS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    public static SimpleDateFormat myTime1 = new SimpleDateFormat("yyyyMMdd");

    static {
        Sec.setLenient(false);
        Min.setLenient(false);
        common.setLenient(false);
        commonLong.setLenient(false);
        commonShort.setLenient(false);
        myTime1.setLenient(false);
    }

    /**
     * 日期转字符串
     *
     * @category 格式化时间精确到秒-24小时制 yyyy-MM-dd HH:mm:ss
     */
    public static String formatDateForSec(Date date) {
        String tempStr = null;
        if (date != null) {
            tempStr = Sec.format(date);
        }
        return tempStr;
    }

    /**
     * 日期转字符串
     *
     * @category 格式化时间精确到分-24小时制 yyyy-MM-dd HH:mm
     */
    public static String formatDateForMin(Date date) {
        String tempStr = null;
        if (date != null) {
            tempStr = Min.format(date);
        }
        return tempStr;
    }

    /**
     * 日期转字符串
     *
     * @category 格式化时间精确到日 yyyy-MM-dd
     */
    public static String formatDateForDay(Date date) {
        String tempStr = null;
        if (date != null) {
            tempStr = common.format(date);
        }
        return tempStr;
    }

    /**
     * 日期转字符串
     *
     * @category 格式化时间精确到日-缩写yyyy-mm-dd
     */
    public static String formatDateForDayLong(Date date) {
        String tempStr = null;
        if (date != null) {
            tempStr = commonLong.format(date);
        }
        return tempStr;
    }

    /**
     * 日期转字符串
     *
     * @category 格式化时间精确到日-缩写 mm-dd
     */
    public static String formatDateForDayShort(Date date) {
        String tempStr = null;
        if (date != null) {
            tempStr = commonShort.format(date);
        }
        return tempStr;
    }

    /**
     * 日期转字符串
     *
     * @category 格式化时间精确到日 -- MM-dd
     */
    public static String formatDateForDayShort2(Date date) {
        String tempStr = null;
        if (date != null) {
            tempStr = dayFormat.format(date);
        }
        return tempStr;
    }

    /**
     * 字符串转日期
     *
     * @category 格式化时间精确到秒-24小时制 yyyy-MM-dd HH:mm:ss
     */
    public static Date formatDateForSec(String dateStr) {
        Date tempDate = null;
        try {
            if (notEmpty(dateStr)) {
                tempDate = Sec.parse(dateStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempDate;
    }

    /**
     * 字符串转日期
     *
     * @category 格式化时间精确到分-24小时制 yyyy-MM-dd HH:mm
     */
    public static Date formatDateForMinToDate(String dateStr) {
        Date tempDate = null;
        try {
            if (notEmpty(dateStr)) {
                tempDate = Min.parse(dateStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempDate;
    }

    /**
     * 字符串转日期
     *
     * @category 格式化时间精确到日 yyyy-MM-dd
     */
    public static Date formatDateForDay(String dateStr) {
        Date tempDate = null;
        try {
            if (notEmpty(dateStr)) {
                tempDate = common.parse(dateStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempDate;
    }

    /**
     * 长整数转字符串
     *
     * @category 格式化时间精确到秒 24小时制 yyyy-MM-dd HH:mm:ss
     */
    public static String formatDateForSec(long num) {
        String tempTimeStr = "";
        if (num != 0) {
            tempTimeStr = Sec.format(new Date(num));
        }
        return tempTimeStr;
    }

    /**
     * 长整数转字符串
     *
     * @category 格式化时间精确到分 24小时制 yyyy-MM-dd HH:mm
     */
    public static String formatDateForMin(long num) {
        String tempTimeStr = "";
        if (num != 0) {
            tempTimeStr = Min.format(new Date(num));
        }
        return tempTimeStr;
    }

    /**
     * 长整数转字符串
     *
     * @category 格式化时间精确到日 yyyy-MM-dd
     */
    public static String formatDateForDayToString(long num) {
        String tempTimeStr = "";
        if (num != 0) {
            tempTimeStr = common.format(new Date(num));
        }
        return tempTimeStr;
    }

    /**
     * 长整数转日期
     *
     * @category 格式化时间精确到日 yyyy-MM-dd
     */
    public static Date formatDateForDay(long num) {
        String tempTimeStr = "";
        if (num != 0) {
            tempTimeStr = formatDateForDayToString(num);
        }
        return formatDateForDay(tempTimeStr);
    }

    /**
     * 长整数转字符串
     *
     * @category 格式化时间精确到日 -- mm月dd日
     */
    public static String formatDateForDayShort(long num) {
        String tempTimeStr = "";
        if (num != 0) {
            tempTimeStr = commonShort.format(new Date(num));
        }
        return tempTimeStr;
    }

    /**
     * 长整数转字符串
     *
     * @category 用户自定义日期格式
     */
    public static String formatDateDefine(String format, long num) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat(format);
        String tempTimeStr = "";
        if (num != 0) {
            tempTimeStr = simpleFormat.format(new Date(num));
        }
        return tempTimeStr;
    }

    /**
     * 字符串转日期
     *
     * @category 用户自定义日期格式
     */
    public static Date formatDateDefine(String format, String dateStr) {
        SimpleDateFormat simFormat = new SimpleDateFormat(format);
        Date tempTimeStr = null;
        try {
            tempTimeStr = simFormat.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempTimeStr;
    }

    /**
     * 日期转字符串
     *
     * @category 用户自定义日期格式
     */
    public static String formatDateDefine(String format, Date dateStr) {
        SimpleDateFormat simFormat = new SimpleDateFormat(format);
        String tempTimeStr = "";
        try {
            if (dateStr != null) {
                tempTimeStr = simFormat.format(dateStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempTimeStr;
    }

    /**
     * @category 获取当前时间日期
     */
    public static String CurrentTimeMillisToString() {
        return formatDateForSec(new Date().getTime());
    }

    /**
     * @category 获取当前时间日期-自定义
     */
    public static String CurrentTimeMillis(String format) {
        return formatDateDefine(format, System.currentTimeMillis());
    }

    /**
     * @category 获取当前时间日期
     */
    public static long CurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * @category 获取指定年的年份
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * @category 返回指定年的月份
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * @category 返回指定年的日
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /**
     * @category 获取指定日期为星期
     */
    public static String getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return WEEKS[week_index];
    }

    /**
     * @category 返回指定日期的小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * @category 返回指定日期的分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * @category 返回指定日期的秒
     */
    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * @category 返回指定日期毫秒
     */
    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    /**
     * @category 获取指定日期为某年的第几周
     */
    public static int getWeekOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * @category 获取当前年
     */
    public static int getYear() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.YEAR);
    }

    /**
     * @category 返回当前系统月份
     */
    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * @category 返回当前系统日
     */
    public static int getDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.DATE);
    }

    /**
     * @category 获取当前系统星期
     */
    public static String getWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int week_index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return WEEKS[week_index];
    }

    /**
     * @category 返回当前小时
     */
    public static int getHour() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * @category 返回当前系统分钟
     */
    public static int getMinute() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * @category 返回当前系统秒
     */
    public static int getSecond() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.SECOND);
    }

    /**
     * @category 返回当前系统毫秒
     */
    public static long getMillis() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTimeInMillis();
    }

    /**
     * @category 获取当前系统时间为某年的第几周
     */
    public static int getWeekOfYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * @category 得到某年的某个月有多少天
     */
    public static int getDaysOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days_of_month;
    }

    /**
     * @category 特殊处理
     */
    public static String formatDateForMin(String dateStr) {
        String tempTimeStr = "";
        if (notEmpty(dateStr)) {
            Calendar ca = Calendar.getInstance();
            ca.setTimeInMillis(Long.parseLong(dateStr));
            tempTimeStr = Min.format(ca.getTime());
        }
        return tempTimeStr;
    }

    /**
     * @category 求两个日期相差天数
     */
    public static int getTimeDifferenceDay(String newTime, String oldTime) {
        int timeDifference = 0;
        try {
            Date newDate = Sec.parse(newTime);
            Date oldDate = Sec.parse(oldTime);
            timeDifference = Math.abs((int) ((newDate.getTime() - oldDate.getTime()) / (24 * 60 * 60 * 1000)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDifference;
    }

    /**
     * @category 求两个日期相差天数
     */
    public static int getTimeDifferenceDay(Date newTime, Date oldTime) {
        int timeDifference = Math.abs((int) ((newTime.getTime() - oldTime.getTime()) / (24 * 60 * 60 * 1000)));
        return timeDifference;
    }

    /**
     * @category 求两个日期相差小时
     */
    public static int getTimeDifferenceHour(String newTime, String oldTime) {
        int timeDifference = 0;
        try {
            Date newDate = Sec.parse(newTime);
            Date oldDate = Sec.parse(oldTime);
            timeDifference = Math.abs((int) ((newDate.getTime() - oldDate.getTime()) / (60 * 60 * 1000)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDifference;
    }

    /**
     * @category 求两个日期相差小时
     */
    public static int getTimeDifferenceHour(Date newTime, Date oldTime) {
        int timeDifference = Math.abs((int) ((newTime.getTime() - oldTime.getTime()) / (60 * 60 * 1000)));
        return timeDifference;
    }

    /**
     * @category 求两个日期相差分钟
     */
    public static int getTimeDifferenceMin(String newTime, String oldTime) {
        int timeDifference = 0;
        try {
            Date newDate = Sec.parse(newTime);
            Date oldDate = Sec.parse(oldTime);
            timeDifference = Math.abs((int) ((newDate.getTime() - oldDate.getTime()) / (60 * 1000)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDifference;
    }

    /**
     * @category 求两个日期相差分钟
     */
    public static int getTimeDifferenceMin(Date newTime, Date oldTime) {
        int timeDifference = Math.abs((int) ((newTime.getTime() - oldTime.getTime()) / (60 * 1000)));
        return timeDifference;
    }

    /**
     * @category 求两个日期相差秒数
     */
    public static int getTimeDifferenceSec(String newTime, String oldTime) {
        int timeDifference = 0;
        try {
            Date newDate = (Date)Sec.parseObject(newTime);
            Date oldDate = (Date)Sec.parseObject(oldTime);
            timeDifference = Math.abs((int) ((newDate.getTime() - oldDate.getTime()) / (1000)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDifference;
    }

    /**
     * @category 求两个日期相差秒数
     */
    public static int getTimeDifferenceSec(Date newTime, Date oldTime) {
        int timeDifference = Math.abs((int) ((newTime.getTime() - oldTime.getTime()) / (1000)));
        return timeDifference;
    }

    /**
     * @category 获得所在星期的第一天
     */
    public static String getFirstDateByWeek(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        int today = now.get(Calendar.DAY_OF_WEEK);
        int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
        now.set(now.DATE, first_day_of_week);
        return common.format(now.getTime());
    }

    /**
     * @category 获得所在星期的最后一天
     */
    public static String getLastDateByWeek(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        int today = now.get(Calendar.DAY_OF_WEEK);
        int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
        int last_day_of_week = first_day_of_week + 6; // 星期日
        now.set(now.DATE, last_day_of_week);
        return common.format(now.getTime());
    }

    /**
     * @category 获得指定日期推前几天的日期
     * 例子：getOrderDay(new Date(), 7) 获取7天前的日期
     */
    public static String getOrderDay(Date currentDate, int day) {
        long newTime = 0;
        if (day != 1) {
            if (day >= 20) {
                long t1 = Math.abs(day / 2 * 24 * 60 * 60 * 1000);
                newTime = currentDate.getTime() - t1 - t1;
            } else {
                long sevenDay = Math.abs(day * 24 * 60 * 60 * 1000);
                newTime = currentDate.getTime() - sevenDay;
            }
        } else {
            return CurrentTimeMillis("yyyy-MM-dd");
        }
        return formatDateForDayToString(newTime);
    }

    public static String getOrderDay2(Date currentDate, int day) {
        long newTime = 0;
        long sevenDay = Math.abs(day * 24 * 60 * 60 * 1000);
        newTime = currentDate.getTime() - sevenDay;
        return formatDateForDayToString(newTime);
    }

    /**
     * @category 获得指定月份的最后一天
     */
    public static String getLastDateByMonth(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1);
        now.set(Calendar.DATE, 1);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - 1);
        now.set(Calendar.HOUR, 11);
        now.set(Calendar.MINUTE, 59);
        now.set(Calendar.SECOND, 59);
        return common.format(now.getTime());
    }

    /**
     * @category 获得指定月份的第一天
     */
    public static String getFirstDateByMonth(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, 0);
        now.set(Calendar.HOUR, 12);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        return common.format(now.getTime());
    }

    /**
     * @category 取当前系统时间设置月份
     */
    public static Date setDateMonth(int month) {
        Calendar now = Calendar.getInstance();
        Date date = new Date();
        now.setTime(date);
        now.set(Calendar.DAY_OF_MONTH, 1);  //设置日期
        now.set(Calendar.MONTH, month - 1);
        return now.getTime();
    }

    /**
     * @category 判断日期是否为工作日(周六和周日为非工作日)
     */
    public static boolean isWorkDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        if (week == Calendar.SUNDAY || week == Calendar.SATURDAY) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @param firstBegin 开始时间段开始
     * @param firstEnd   开始时间段结束
     * @param LastBegin  结束时间段开始
     * @param LastEnd    结束时间段结束
     * @category 返回两个时间段得相交的天数 格式yyyy-MM-dd
     */
    public static long getDays(String firstBegin, String firstEnd, String LastBegin, String LastEnd) {
        long ret = 0;
        String begindate = "";
        String enddate = "";
        try {
            Calendar begin1 = Calendar.getInstance();
            Calendar end1 = Calendar.getInstance();
            Calendar begin2 = Calendar.getInstance();
            Calendar end2 = Calendar.getInstance();
            begin1.setTime(common.parse(firstBegin));
            end1.setTime(common.parse(LastBegin));
            begin2.setTime(common.parse(firstEnd));
            end2.setTime(common.parse(LastEnd));
            //时间段不相交
            if ((begin2.getTime().getTime() > end1.getTime().getTime() && end2.getTime().getTime() > end1.getTime().getTime()) ||
                    (begin2.getTime().getTime() < begin1.getTime().getTime() && end2.getTime().getTime() < begin1.getTime().getTime())) {
                //System.out.println("b"+ret);
                return ret;
            }
            if (begin2.getTime().getTime() >= begin1.getTime().getTime()) {
                begindate = common.format(begin2.getTime());
            } else {
                begindate = common.format(begin1.getTime());
            }
            if (end2.getTime().getTime() >= end1.getTime().getTime()) {
                enddate = common.format(end1.getTime());
            } else {
                enddate = common.format(end2.getTime());
            }
            if (!begindate.equals("") && !enddate.equals("")) {
                ret = getTimeDifferenceDay(begindate, enddate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * @category 比较日期
     * 格式为yyyy-MM-dd的<br>
     * 若d1小于d2返回true<br>
     * d1=2007-10-01,d2=2007-10-15,则返回true
     */
    public static boolean after(String d1, String d2) {
        try {
            Date dt1 = common.parse(d1);
            Date dt2 = common.parse(d2);
            return dt1.getTime() < dt2.getTime();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @param date 原日期
     * @param len  移动量
     * @return 移动后日期
     * @category 移动日期
     */
    public static String dayMove(String date, int len) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(common.parse(date));
            cal.add(Calendar.DATE, len);
            return common.format(cal.getTime());
        } catch (Exception e) {
            return date;
        }
    }

    /**
     * @param date 原日期
     * @param len  移动量
     * @return 移动后日期
     * @category 移动日期
     */
    public static Date dayMove(Date date, int len) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DATE, len);
            return cal.getTime();
        } catch (Exception e) {
            return date;
        }
    }

    /**
     * @param date 原日期
     * @param len  移动量
     * @return 移动后日期
     * @category 移动月份
     */
    public static String monthMove(String date, int len) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(common.parse(date));
            cal.add(Calendar.MONTH, len);
            return common.format(cal.getTime());
        } catch (Exception e) {
            return date;
        }
    }

    /**
     * @param date 原日期
     * @param len  移动量
     * @return 移动后日期
     * @category 移动秒
     */
    public static String secondMove(String date, int len) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(Sec.parse(date));
            cal.add(Calendar.SECOND, len);
            return Sec.format(cal.getTime());
        } catch (Exception e) {
            return date;
        }
    }

    public static void main(String[] args) {
        System.out.println(DateFormatUtil.formatDateForSec(new Date()));
        String date = secondMove(DateFormatUtil.formatDateForSec(new Date()),7000);
        System.out.println(date);
    }

    /**
     * @category 判断一个字符串是否不为空或者不是右空白符号组成
     */
    private static boolean notEmpty(String source) {
        return (source != null && source.trim().length() > 0) ? true : false;
    }

    /**
     * 20140730
     *
     * @param date
     * @return
     */
    public static String myTime1(Date date) {
        String tempStr = null;
        if (date != null) {
            tempStr = myTime1.format(date);
        }
        return tempStr;
    }

    /**
     *  * @category getTimeMoveForMysql:获取某个时间偏移后的用于mysql时间判断
     *  *
     *  * @author yorke
     *  * @param date
     *  * @param m
     *  * @return
     *  * @since JDK 1.6
     *  
     */
    public static long getTimeMoveForMysql(Date date, int m, long lastTime) {
        long currentTime = date.getTime();
        long sevenDaysAgo = currentTime - (m * 24 * 60 * 60 * 1000);
        if (lastTime == 0 || lastTime < sevenDaysAgo) {
            lastTime = sevenDaysAgo;
        }
        return lastTime;
    }

    public static String getTimeDifferenceStr(Date newTime, Date oldTime) {
        //相差多少分钟
        int i = DateFormatUtil.getTimeDifferenceMin(newTime, oldTime);
        String iStr = i + "分钟前";
        if (i > 60) {
            //相差多少小时
            i = DateFormatUtil.getTimeDifferenceHour(newTime, oldTime);
            iStr = i + "小时前";
            if (i > 24) {
                //相差多天小时
                i = DateFormatUtil.getTimeDifferenceDay(newTime, oldTime);
                iStr = i + "天前";
                if (i > 7) {
                    iStr = "7天前";
                }
            }
        }
        return iStr;
    }

    /**
     *  * getTimeDifferenceTodayStr:1内的显示时间，1天后开始显示多少天前
     *  *
     *  * @author yorke
     *  * @param date
     *  * @return
     *  * @since JDK 1.6
     *  
     */
    public static String getTimeDifferenceTodayStr(Date date) {
        Date curDate = new Date();
        String iStr = "";
        //今天开始
        try {
            int i = DateFormatUtil.getTimeDifferenceMin(curDate, date);
            iStr = i + "分钟前";
            if (i == 0) {
                iStr = "刚刚";
            }
            if (i > 60) {
                //相差多少小时
                i = DateFormatUtil.getTimeDifferenceHour(curDate, date);
                iStr = i + "小时前";
                if (i >= 24) {
                    //相差多天小时
                    i = DateFormatUtil.getTimeDifferenceDay(curDate, date);
                    iStr = "昨天";
                    if (i >= 2) {
                        //判断是否当年
                        int thisY = DateFormatUtil.getYear();
                        //评论时间
                        int postY = DateFormatUtil.getYear(date);
                        if (thisY == postY) {
                            iStr = DateFormatUtil.formatDateDefine("MM-dd HH:mm", date);
                        } else {
                            iStr = DateFormatUtil.formatDateDefine("yyyy-MM-dd HH:mm", date);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iStr;
    }

    /**
     * @param s
     * @return String    时间秒转变为分钟、秒的组合时间字符串
     * @Title: getTransferTimeBySecondTime
     * @Description: 获取时间秒转变为分钟、秒的组合时间字符串
     */
    public static String getTransferTimeBySecondTime(int s) {
        String transferTimeStr = "0";
        if (s > 0) {
            String outMinute = s / 60 + "";
            String outSecond = s % 60 + "";
            if (outMinute.length() == 1) {
                outMinute = "0" + outMinute;
            }
            if (outSecond.length() == 1) {
                outSecond = "0" + outSecond;
            }
            transferTimeStr = outMinute + ":" + outSecond;
        }
        return transferTimeStr;
    }

    /**
     * @param min
     * @param max
     * @return int    返回类型
     * @throws
     * @Title: getRandomScopeInteger
     * @Description: 获取min到max的随机数
     */
    public static int getRandomScopeInteger(int min, int max) {
        Random random = new Random();
        int randomInteger = random.nextInt(max) % (max - min + 1) + min;
        return randomInteger;
    }

    //得到几天后的时间,返回日期，不包括当天
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }


    /**
     * 某个时间到当前时间间隔多长时间
     *
     * @param contrastTime 待比较时间
     * @return
     */
    public static String getTimeInterval(Date contrastTime) {
        String timeIntervalStr = "0秒";
        Date currentTime = new Date();
        if (contrastTime != null) {
            int intervalDay = DateFormatUtil.getTimeDifferenceDay(contrastTime, currentTime);
            if (intervalDay != 0) {
                timeIntervalStr = intervalDay + "天";
                int intervalHour = DateFormatUtil.getTimeDifferenceHour(contrastTime, currentTime) - intervalDay * 24;
                if (intervalHour != 0) {
                    timeIntervalStr += intervalHour + "小时";
                    int intervalMin = DateFormatUtil.getTimeDifferenceMin(contrastTime, currentTime) - intervalDay * 24 * 60 - intervalHour * 60;
                    if (intervalMin != 0) {
                        timeIntervalStr += intervalMin + "分";
                    } else {
                    }
                } else {
                    int intervalMin = DateFormatUtil.getTimeDifferenceMin(contrastTime, currentTime) - intervalDay * 24 * 60;
                    if (intervalMin != 0) {
                        timeIntervalStr += intervalMin + "分";
                    } else {
                    }
                }
            } else {
                int intervalHour = DateFormatUtil.getTimeDifferenceHour(contrastTime, currentTime);
                if (intervalHour != 0) {
                    timeIntervalStr = intervalHour + "小时";
                    int intervalMin = DateFormatUtil.getTimeDifferenceMin(contrastTime, currentTime) - intervalHour * 60;
                    if (intervalMin != 0) {
                        timeIntervalStr += intervalMin + "分";
                    } else {
                    }
                } else {
                    int intervalMin = DateFormatUtil.getTimeDifferenceMin(contrastTime, currentTime);
                    if (intervalMin != 0) {
                        timeIntervalStr = intervalMin + "分";
                    } else {
                    }
                }
            }
        }
        return timeIntervalStr;
    }


    public static String getTimeDifferenceStrV2(Date newTime, Date oldTime) {
        //相差多少分钟
        int i = DateFormatUtil.getTimeDifferenceMin(newTime, oldTime);
        if (i < 1) {
            return "刚刚";
        }
        String iStr = i + "分钟前";
        if (i > 60) {
            //相差多少小时
            i = DateFormatUtil.getTimeDifferenceHour(newTime, oldTime);
            iStr = i + "小时前";
            if (i > 24) {
                //相差多天小时
                i = DateFormatUtil.getTimeDifferenceDay(newTime, oldTime);
                iStr = i + "天前";
                if (i > 7) {
                    iStr = "7天前";
                }
            }
        }
        return iStr;
    }

    /**
     * @param afferentDate
     * @return String
     * 同一天返回 13：00
     * 昨天返回	昨天
     * 前天返回 前天
     * 前天以上同年返回 03-15
     * 不同年返回	2000-03-15
     */
    public static String getMultiformDate(Date afferentDate) {
        String resultString = null;
        Date currentTime = new Date();
        SimpleDateFormat sdf = null;
        Calendar ca = Calendar.getInstance();
        ca.setTime(currentTime);
        int currentTimeYear = ca.get(Calendar.YEAR);
        int currentTimeMONTH = ca.get(Calendar.MONTH);
        int currentTimeDATE = ca.get(Calendar.DATE);
        ca.setTime(afferentDate);
        int afferentDateYear = ca.get(Calendar.YEAR);
        int afferentDateMONTH = ca.get(Calendar.MONTH);
        int afferentDateDATE = ca.get(Calendar.DATE);
        if (afferentDateYear == currentTimeYear) {//同年
            if (afferentDateMONTH < currentTimeMONTH) {//同年不同月
                sdf = new SimpleDateFormat("MM-dd");
                resultString = sdf.format(afferentDate);
            } else {//同月
                if (afferentDateDATE == currentTimeDATE) {//同日
                    sdf = new SimpleDateFormat("HH:mm");
                    resultString = sdf.format(afferentDate);
                } else if (afferentDateDATE + 1 == currentTimeDATE) {
                    resultString = "昨天";
                } else if (afferentDateDATE + 2 == currentTimeDATE) {
                    resultString = "前天";
                } else {
                    sdf = new SimpleDateFormat("MM-dd");
                    resultString = sdf.format(afferentDate);
                }
            }
        } else {//不同年
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            resultString = sdf.format(afferentDate);
        }
        return resultString;
    }

    /**
     * 返回当天的最后一秒
     * 传 2018-01-02 会返回 2018-01-02 23:59
     *
     * @param endDate
     * @return String
     */
    public static String getLastSecond(String endDate) {
        SimpleDateFormat sdf = null;
        String updateStr = null;
        if (endDate == null){
            return  null;
        }
        try {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(endDate);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.add(Calendar.DATE, 1);
            ca.add(Calendar.MILLISECOND, -1);
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date updateDate = ca.getTime();
            updateStr = sdf.format(updateDate);
        } catch (ParseException e) {
            updateStr = endDate;
        }
        return updateStr;
    }

    /**
     * 日期正则表达式
     * 9018-01-02 23:59:59
     * 9018-01-02
     *
     * @param dateStr
     * @return Boolean
     */
    public static Boolean dateRegexp(String dateStr) {
        Boolean resultFlag = false;
        String ymd = "[1-9][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])";
        String ymdhms = "[1-9][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):[0-5][0-9]";
        Pattern patternYmd = Pattern.compile(ymd);
        Matcher matcherYmd = patternYmd.matcher(dateStr);
        Boolean flag = matcherYmd.matches();

        Pattern patternYmdhms = Pattern.compile(ymdhms);
        Matcher matcherYmdhms = patternYmdhms.matcher(dateStr);
        Boolean flags = matcherYmdhms.matches();
        if (flag || flags) {
            resultFlag = true;
        }
        return resultFlag;
    }

    /**
     * @category 比较日期
     * 格式为yyyy-MM-dd HH:mm:ss的<br>
     * 若d1小于d2返回true<br>
     * d1=2007-10-01 00:00:00,d2=2007-10-15  00:00:00,则返回true
     */
    public static boolean compareTimeSec(String d1, String d2) {
        try {
            Date dt1 = Sec.parse(d1);
            Date dt2 = Sec.parse(d2);
            return dt1.getTime() < dt2.getTime();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @param date
     * @return return
     * 今天  ---0
     * 昨天--1
     * 3天内  --<=3
     * 7天内   --<=7
     * 1周前    -->7
     * 2周前      -->14  不包含 两周
     * 1个月前     -->30
     * 3个月前     -->90
     * 半年前      -->180
     * 1年前        ---->365
     * 2年前         --->365*2
     * 3年前          --->365*3
     * @category 用文字描述传入时间和今天的差值
     */
    public static String formatDate(Date date) {
        String resultStr = null;
        if (date == null) {
            return resultStr;
        }
        Date nowDate = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        long datetimeInt = ca.getTimeInMillis();
        ca.setTime(nowDate);
        long nowdatetimeInt = ca.getTimeInMillis();
        int day = (int) ((nowdatetimeInt - datetimeInt) / 1000 / 60 / 60 / 24);
        if(day == 0){
            resultStr = "今天";
            return resultStr;
        }
        if( day == 1) {
            resultStr = "昨天";
            return resultStr;
        }
        if (7 >= day) {
            if (3 >= day) {
                resultStr = "三天内";
            }else {
                resultStr = "七天内";
            }
        }else if (365*3 >= day){
            if (7*2 >= day) {
                //七天前14天内包括14天
                resultStr = "一周前";
            } else if (30 >= day){
                //14天前 30天内
                resultStr = "两周前";
            } else if (3*30 >= day) {
                //一个月前 三个月内
                resultStr = "一个月前";
            } else if (6*30 >= day) {
                //三个月前  半年内
                resultStr = "三个月前";
            } else if (365 >= day) {
                //半年前 一年内
                resultStr = "半年前";
            } else if (365*2 >= day) {
                //一年前 两年内
                resultStr = "一年前";
            } else if (365*3 >= day) {
                //两年前  三年内
                resultStr = "两年前";
            }
        } else {
            resultStr = "三年前";
        }
        return resultStr;
    }

    public void fun() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse("2018-5-17");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.print(formatDate(date));
    }

    /*public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse("1996-10-05");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        formatDate(date);
    }*/


    /**
     * @param date
     * @param amount   差值数量
     * @param dateType 时间维度  1：年 2：月 3：日 4：周
     * @return resultDate
     * @category 返回传入时间的 amount 年【月、日】前的日期字符串
     */
    public static Date lastDate(Date date, int amount, int dateType) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        if (dateType == 1) {
            ca.add(Calendar.YEAR, -amount);
        }
        if (dateType == 2) {
            ca.add(Calendar.MONTH, -amount);
        }
        if (dateType == 3) {
            ca.add(Calendar.DAY_OF_YEAR, -amount);
        }
        if (dateType == 4) {
            ca.add(Calendar.WEEK_OF_YEAR, -amount);
        }
        Date resultDate = ca.getTime();
        return resultDate;
    }

    /**
     * @param date
     * @param dateType 时间维度  1：年 2：年月 3：年月日 4：年月周 5：月日6：月
     * @return dateStr
     * @category 时间格式化
     * （按天的就显示2018年5月13日，按周的就显示2018年5月第3周，按月就显示2018年5月，按年就显示2018年）
     */
    public static String statusFormat(Date date, int dateType) {
        SimpleDateFormat sdf = null;
        Calendar ca = null;
        String dateStr = null;
        if (dateType == 1) {
            sdf = new SimpleDateFormat("yyyy年");
            dateStr = sdf.format(date);
        }
        if (dateType == 2) {
            sdf = new SimpleDateFormat("yyyy年MM月");
            dateStr = sdf.format(date);
        }
        if (dateType == 3) {
            sdf = new SimpleDateFormat("yyyy年MM月dd日");
            dateStr = sdf.format(date);
        }
        if (dateType == 4) {
            sdf = new SimpleDateFormat("yyyy年MM月");
            String formatDate = sdf.format(date);
            ca = Calendar.getInstance();
            ca.setTime(date);
            int aFewWeeks = ca.get(Calendar.WEEK_OF_MONTH);
            dateStr = formatDate + "第" + aFewWeeks + "周";
        }
        if (dateType == 5) {
            sdf = new SimpleDateFormat("MM月dd日");
            dateStr = sdf.format(date);
        }
        if (dateType == 6) {
            sdf = new SimpleDateFormat("MM月");
            dateStr = sdf.format(date);
        }
        return dateStr;
    }

    /**
     * @param date
     * @param dateType 0:表示一周的开始  1:表示一周的结束
     * @return returnDate
     * @category 返回当前周的第一天时间--星期日和最后一天--星期六
     */
    public static Date beginWeekOrEndWeek(Date date, int dateType) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        //获取当前时间是一周的第几天
        int aFewWeeks = ca.get(Calendar.DAY_OF_WEEK);
        if (dateType == 0) {
            ca.add(Calendar.DAY_OF_YEAR, -aFewWeeks + 1);
        }
        if (dateType == 1) {
            ca.add(Calendar.DAY_OF_YEAR, -aFewWeeks + 7);
        }
        Date returnDate = ca.getTime();
        return returnDate;
    }

    /**
     * @param date
     * @param dateType 0:表示一个月的开始  1:表示一个月的结束
     * @return int
     * @category 返回当前月的第一天或者最后一天
     */
    public static Date beginMonthOrEndMonth(Date date, int dateType) {
        Date resultDate = null;
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int actualMaximum = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
        //这个月的第几天
        int dayOfMonth = ca.get(Calendar.DAY_OF_MONTH);
        if(dateType == 0){
            //一个月的第一天
            ca.add(Calendar.DAY_OF_YEAR, -dayOfMonth+1);
        }
        if(dateType == 1){
            //一个月的最后一天
            ca.add(Calendar.DAY_OF_YEAR, -dayOfMonth + actualMaximum);
        }
        resultDate = ca.getTime();
        return resultDate;
    }

    /**
     * @param date
     * @return falg
     * @category 判断是否为今年
     */
    public static Boolean isSameYear(Date date) {
        Boolean flag = false;
        Date nowDate = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(nowDate);
        int nowYear = ca.get(Calendar.YEAR);
        ca.setTime(date);
        int year = ca.get(Calendar.YEAR);
        if (year == nowYear) {
            flag = true;
        }
        return flag;
    }

    /**
     * @category 时间的时分和传入的字符串做比较 当前日期大于等于指定日期返回true
     * @param date 当前日期
     * @param appointDate 指定日期  格式 8:30
     * @return falg
     */
    public static Boolean comparativeTimeDivision (Date date, String appointDate) {
        if(appointDate == null){
            return true;
        }
        Boolean flag = false;
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int hourDate = ca.get(Calendar.HOUR_OF_DAY);
        int minuteDate = ca.get(Calendar.MINUTE);
        String[] hourndAndMinnute = StringUtil.split(appointDate,":");
        String hourAppointDateStr = hourndAndMinnute[0];
        String minuteAppointDateStr = hourndAndMinnute[1];
        int hourAppointDate = Integer.parseInt(hourAppointDateStr);
        int minuteAppointDate = Integer.parseInt(minuteAppointDateStr);
        //如果大于等于指定时
        if (hourDate > hourAppointDate) {
            flag = true;
        }
        if (hourDate == hourAppointDate) {
            if (minuteDate >= minuteAppointDate) {
                flag = true;
            }
        }
        return  flag;
    }

    /**
     * @category 时间的周的维度和指定做比较 当前日期大于等于指定日期返回true
     * @param date 当前日期
     * @param appointDate 指定日期  格式 1234567 对应日一二三四五六
     * @return falg
     */
    public static Boolean weeklyJudgment (Date date, String appointDate) {
        if(appointDate == null){
            return true;
        }
        Boolean flag = false;
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int dayOfWeek = ca.get(Calendar.DAY_OF_WEEK);
        int appointDateInt = Integer.parseInt(appointDate);
        //如果大于等于指定时
        if (dayOfWeek >= appointDateInt) {
            flag = true;
        }
        System.out.print(flag);
        return  flag;
    }

    /**
     * @category 时间的周的维度和指定做比较 当前日期大于等于指定日期返回true
     * @param date 当前日期
     * @param appointDate    号数 1-31
     * @return falg
     */
    public static Boolean monthJudgment (Date date, String appointDate) {
        if(appointDate == null){
            return true;
        }
        Boolean flag = false;
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int dayOfMonth = ca.get(Calendar.DAY_OF_MONTH);
        int appointDateInt = Integer.parseInt(appointDate);
        //如果大于等于指定时
        if (dayOfMonth >= appointDateInt) {
            flag = true;
        }
        System.out.print(flag);
        return  flag;
    }

    /**
     * @category不同年就加上年份
     * @param resultDate
     * @return String DateStr
     */
    public static String addYear(Date resultDate) {
        String DateStr = null;
        if (DateFormatUtil.isSameYear(resultDate)) {
            //同年
            DateStr = DateFormatUtil.statusFormat(resultDate, 5);
        } else {
            DateStr = DateFormatUtil.statusFormat(resultDate, 1)
                    + DateFormatUtil.statusFormat(resultDate, 5);
        }
        return DateStr;
    }
}
